package org.yray.somehttp.se.hsex;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.*;

import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.platform.Platform;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;


@Slf4j
public class OkHttpTool {

    public static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
    public static final String AUTH_FILE_URL = "AUTH_FILE_URL";//配置的地址

    public static String get(String url) {
        String result = "";
        Response response =null;
        try {
            SSLContext context=loadCer();
            X509TrustManager trustManager = Platform.get().platformTrustManager();
            Request request = new Request.Builder().url(url).get()
                    .addHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36").build();
            OkHttpClient client = new OkHttpClient.Builder()
                    .sslSocketFactory(context.getSocketFactory(),trustManager)
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .hostnameVerifier(new TrustAnyHostnameVerifier())
                    .build();

            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                result = response.body().string();
            } else {
                throw new IOException("Unexpected code " + response);
            }
            log.info("{}",result);
            response.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(response!=null){
              //  IOUtils.close(response);
            }
        }
        return result;
    }

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }


    public static SSLContext loadCer() throws Exception {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream caInput = new BufferedInputStream(new FileInputStream("D://cert/hsex.cer"));
        Certificate ca = cf.generateCertificate(caInput);
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
        SSLContext context = SSLContext.getInstance("TLSv1.3");
        context.init(null, tmf.getTrustManagers(), null);
        return context;
    }

    public static void main(String[] args) {
        String p=System.getProperty ("https.protocols");
        log.info("p={}",p);
        System.setProperty ("https.protocols", "TLSv1.2,TLSv1.1,TLSv1.3,SSLv3,DVSSL");
        String result=OkHttpTool.get("https://hsex.men/video-790815.htm");
        log.info("{}",result);
    }
}